package com.forlove.app.qselect.friend.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.forlove.app.R;
import com.forlove.app.common.base.ui.BaseActivity;
import com.forlove.app.common.widget.recyview.BaseRecyclerAdapter;
import com.forlove.app.manager.FriendManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.friend.adapter.FriendNewAskAdapter;
import com.forlove.app.qselect.friend.entity.ContactsFriendEntity;
import com.forlove.app.qselect.friend.entity.FriendAskEntity;
import com.forlove.app.qselect.friend.entity.MineFriendEntity;
import com.forlove.app.qselect.friend.entity.MineFriendListEntity;
import com.forlove.app.qselect.friend.entity.NewsFriendsStatus;
import com.forlove.app.qselect.friend.entity.SearchFriendEntity;
import com.forlove.app.qselect.friend.presenter.FriendAskPresenter;
import com.forlove.app.qselect.friend.view.FriendAskView;
import com.forlove.app.qselect.index.ui.MineInfoActivity;
import com.forlove.app.qselect.login.widget.ResetPwTipDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * 新的好友请求列表
 * Created by yjl on 2017/11/25.
 */

public class FriendAskActivity extends BaseActivity<FriendAskPresenter>
        implements FriendAskView, View.OnClickListener,
        SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.view_fade_back)
    FrameLayout view_fade_back;

    @BindView(R.id.list_new_friends)
    RecyclerView list_new_friends;

    @BindView(R.id.refresh_new_friend)
    SwipeRefreshLayout refresh_new_friend;

    @BindView(R.id.text_all_ask_num)
    TextView text_all_ask_num;

    @BindView(R.id.view_no_data)
    RelativeLayout view_no_data;// 没有数据

    private List<MineFriendEntity> newAskFriends = new ArrayList<>();//新的好友添加请求
    private FriendNewAskAdapter friendNewAskAdapter; //新的好友请求的
    private ResetPwTipDialog resetPwTipDialog;
    private int optionPos = -1;
    private MineFriendEntity agreeFriend;

    @Override
    public int getLayoutId() {
        return R.layout.activity_firends_ask;
    }

    @Override
    public void initView() {
        if (presenter == null) {
            presenter = new FriendAskPresenter(this);
        }
        view_fade_back.setOnClickListener(this);
        refresh_new_friend.setOnRefreshListener(this);
        refresh_new_friend.setColorSchemeResources(R.color.common_green);
        initNewAskAdapter();
//        requestNewFriend();
        presenter.loadAskFriendData();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        StatusBarCompat.compat(this, Color.parseColor("#ffffff"));
    }

    private void initNewAskAdapter() {
        if (friendNewAskAdapter == null) {
            friendNewAskAdapter = new FriendNewAskAdapter(this, R.layout.item_new_friends,
                    newAskFriends, presenter);
            list_new_friends.setAdapter(friendNewAskAdapter);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            list_new_friends.setLayoutManager(linearLayoutManager);
            friendNewAskAdapter.hideFooterView();
            friendNewAskAdapter.hideEmptyView();
            friendNewAskAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                    //未添加
                    if (position < newAskFriends.size()) {
                        MineFriendEntity friendEntity = newAskFriends.get(position);
                        //添加好友，好友申请页面
                        if (friendEntity.status == NewsFriendsStatus.UNCONFIRM.getCode()
                                || friendEntity.status == NewsFriendsStatus.UNADDED.getCode()) {
                            SearchFriendEntity searchFriendEntity = new SearchFriendEntity();
                            searchFriendEntity.friendEntity = friendEntity;
                            searchFriendEntity.isFriend = false;
                            searchFriendEntity.note_times = friendEntity.charm;
                            Intent intent = new Intent(FriendAskActivity.this, SearchUserInfoActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("friendinfo", searchFriendEntity);
                            bundle.putString("mobile", friendEntity.getAvaliableMobile());
                            intent.putExtras(bundle);
                            startActivity(intent);
                        } else if (friendEntity.status == NewsFriendsStatus.CONFIRMED.getCode()) {
                            Intent intent = new Intent(FriendAskActivity.this, MineInfoActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putString("mobile", friendEntity.mobile);
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    }
                }

                @Override
                public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, final int position) {
                    if (resetPwTipDialog == null) {
                        resetPwTipDialog = new ResetPwTipDialog(FriendAskActivity.this);
                    }
                    resetPwTipDialog.setTextMesseage("确定删除该好友记录么？");
                    resetPwTipDialog.setAgreeListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (position < newAskFriends.size()) {
                                MineFriendEntity friendEntity = newAskFriends.get(position);
                                optionPos = position;
                                requestDeleteFriendRecord(friendEntity.mobile);
                            }
                            resetPwTipDialog.dismiss();
                        }
                    });
                    resetPwTipDialog.show();
                    return false;
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_fade_back:
                onBackPressed();
                break;
        }
    }

    @Override
    public void onRefresh() {
//        requestNewFriend();
        presenter.loadAskFriendData();
        if (mHandler == null) {
            mHandler = new Handler(getMainLooper());
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    refresh_new_friend.setRefreshing(false);
                }
            }, 6000);
        }
    }

    private void requestDeleteFriendRecord(String mobile) {
        Map param = new HashMap();
        param.put("mobile", mobile);
        presenter.loadDeleteFriendRecord(param);
    }


    @Override
    public void showErrorData(NetworkManager.ResultCode resultCode, String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        refresh_new_friend.setRefreshing(false);
    }

    @Override
    public void showAgreeFriend(NetworkManager.ResultCode resultCode, String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        if (resultCode.equals(NetworkManager.ResultCode.RESULT_SUCCESS)) {
            //添加好友成功
            presenter.loadAskFriendData();
        }
    }

    @Override
    public void showDeleteRecordView(CommonEntity commonEntity) {
        //删除添加好友信息
//        presenter.loadAskFriendData();
        newAskFriends.remove(optionPos);
//        friendNewAskAdapter.notifyItemRemoved(optionPos);
        Intent intent = getIntent();
        intent.putExtra("pos", optionPos);
        setResult(1, intent);
        friendNewAskAdapter.updateDatas(newAskFriends);
        if (newAskFriends == null || newAskFriends.size() == 0) {
            list_new_friends.setVisibility(View.GONE);
            text_all_ask_num.setVisibility(View.GONE);
            view_no_data.setVisibility(View.VISIBLE);
        } else {
            list_new_friends.setVisibility(View.VISIBLE);
            text_all_ask_num.setVisibility(View.VISIBLE);
            view_no_data.setVisibility(View.GONE);
            text_all_ask_num.setText(String.format(getString(R.string.str_all_ask), newAskFriends.size()));
        }

    }

    @Override
    public void updateContactsFriendView(ContactsFriendEntity contactsFriendEntity) {

    }

    @Override
    public void updateAskFriendView(FriendAskEntity friendAskEntity) {
        refresh_new_friend.setRefreshing(false);
        //有新的好友请求
        if (friendAskEntity.friendsRequest.size() > 0) {
            newAskFriends.clear();
            newAskFriends.addAll(FriendManager.sortFriendRequest(friendAskEntity.friendsRequest));
            friendNewAskAdapter.updateDatas(newAskFriends);
            if (newAskFriends == null || newAskFriends.size() == 0) {
                list_new_friends.setVisibility(View.GONE);
                text_all_ask_num.setVisibility(View.GONE);
                view_no_data.setVisibility(View.VISIBLE);
            } else {
                list_new_friends.setVisibility(View.VISIBLE);
                text_all_ask_num.setVisibility(View.VISIBLE);
                view_no_data.setVisibility(View.GONE);
                text_all_ask_num.setText(String.format(getString(R.string.str_all_ask), friendAskEntity.requestCount));
            }
        }
    }

    @Override
    public void updateFriendListView(MineFriendListEntity mineFriendListEntity) {

    }

    @Override
    public void showScanViewData(SearchFriendEntity searchFriendEntity) {

    }
}
